package icu.smile.proxy.service.impl;

import icu.smile.proxy.dao.MiPuDao;
import icu.smile.proxy.entity.ProxyMiPu;
import icu.smile.proxy.service.MiPuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
import java.util.logging.Logger;

/**
 * <p>
 * TODO
 * </p>
 *
 * @author starrysky
 * @since 2021/6/7
 */
@Service
public class MiPuServiceImpl implements MiPuService {

    @Autowired
    private MiPuDao miPuDao;

    @PersistenceContext
    protected EntityManager entityManager;

    private static final Logger LOGGER = Logger.getLogger(MiPuServiceImpl.class.getName());

    @Override
    @Transactional
    public void save(ProxyMiPu proxyMiPu) {
        ProxyMiPu miPum = new ProxyMiPu();
        miPum.setIp(proxyMiPu.getIp());
        miPum.setPort(proxyMiPu.getPort());
        List<ProxyMiPu> list = this.findAll(miPum);
        if (list.size()==0){
            this.miPuDao.saveAndFlush(proxyMiPu);
        }
    }
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void saveAll(List<ProxyMiPu> entityList){
        miPuDao.saveAll(entityList);



    }
    @Override
    public List<ProxyMiPu> findAll(ProxyMiPu proxyMiPu) {
        Example example = Example.of(proxyMiPu);
        List<ProxyMiPu> list = this.miPuDao.findAll(example);
        return list;
    }
}
